package cc.iteachyou.template.tools;




/**
 * @author duanbowei123
 * @Date:2023/7/6
 * @Description:
 * @version：1.0
 **/
public class StringUtil {

    public static final String EMPTY = "";

    /**
     *  转为string类型
     * @param cs
     * @return
     */
    public static String str(CharSequence cs) {
        return null == cs ? null : cs.toString();
    }

    /**
     *  如果对象是字符串是否为空串，空的定义如下：
     *
     * null
     * 空字符串：""
     * 例：
     * StrUtil.isEmptyIfStr(null) // true
     * StrUtil.isEmptyIfStr("") // true
     * StrUtil.isEmptyIfStr(" \t\n") // false
     * StrUtil.isEmptyIfStr("abc") // false
     * @param obj
     * @return
     */
    public static boolean isEmptyIfStr(Object obj) {
        if (null == obj) {
            return true;
        } else if (obj instanceof CharSequence) {
            return 0 == ((CharSequence) obj).length();
        }
        return false;
    }


    /**
     * 字符串是否为空白，空白的定义如下：
     *  null
     *  ""
     *  空格、全角空格、制表符、换行符，等不可见字符
     *
     *  列
     *  StrUtil.isBlank(null) == true
     *  StrUtil.isBlank("") == true
     *  StrUtil.isBlank(" \t\n") == true
     *  StrUtil.isBlank("abc") ==false
     */
    public static boolean isBlank(CharSequence str) {
        final int length;
        if ((str == null) || ((length = str.length()) == 0)) {
            return true;
        }

        for (int i = 0; i < length; i++) {
            // 只要有一个非空字符即为非空字符串
            if (false == isBlankChar(str.charAt(i))) {
                return false;
            }
        }

        return true;
    }

    /**
     * 检查字符是否为null or 空字符串：""
     * @param str
     * @return
     */
    public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }

    /**
     * 是否空白符 空白符包括空格、制表符、全角空格和不间断空格
     * @param c
     * @return
     */
    public static boolean isBlankChar(char c) {
        return isBlankChar((int) c);
    }

    /**
     * 是否空白符 空白符包括空格、制表符、全角空格和不间断空格
     * @param c
     * @return
     */
    public static boolean isBlankChar(int c) {
        return Character.isWhitespace(c)
                || Character.isSpaceChar(c)
                || c == '\ufeff'
                || c == '\u202a'
                || c == '\u0000'
                // issue#I5UGSQ，Hangul Filler
                || c == '\u3164'
                // Braille Pattern Blank
                || c == '\u2800'
                // MONGOLIAN VOWEL SEPARATOR
                || c == '\u180e';
    }


    /**
     * 与isBlack相反
     * @param str
     * @return
     */
    public static boolean isNotBlank(CharSequence str) {
        return false == isBlank(str);
    }

    /**
     * 就是给定一些字符串，如果一旦有空的就返回true，常用于判断好多字段是否有空的
     * @param strs
     * @return
     */
    public static boolean hasBlank(CharSequence... strs) {
        if (ArrayUtil.isEmpty(strs)) {
            return true;
        }

        for (CharSequence str : strs) {
            if (isBlank(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 去掉指定前缀
     * @param str
     * @param prefix
     * @return
     */
    public static String removePrefix(CharSequence str, CharSequence prefix) {
        if (isEmpty(str) || isEmpty(prefix)) {
            return str(str);
        }

        final String str2 = str.toString();
        if (str2.startsWith(prefix.toString())) {
            return subSuf(str2, prefix.length());// 截取后半段
        }
        return str2;
    }

    public static String removeSuffix(CharSequence str, CharSequence suffix) {
        if (isEmpty(str) || isEmpty(suffix)) {
            return str(str);
        }

        final String str2 = str.toString();
        if (str2.endsWith(suffix.toString())) {
            return subPre(str2, str2.length() - suffix.length());// 截取前半段
        }
        return str2;
    }

    /**
     * 切割指定位置之后部分的字符串
     * @param string
     * @param fromIndex
     * @return
     */
    public static String subSuf(CharSequence string, int fromIndex) {
        if (isEmpty(string)) {
            return null;
        }
        return sub(string, fromIndex, string.length());
    }

    /**
     *切割指定位置之前部分的字符串
     * @param string
     * @param toIndexExclude
     * @return
     */
    public static String subPre(CharSequence string, int toIndexExclude) {
        return sub(string, 0, toIndexExclude);
    }

    /**
     * 针对于String.substring()的改进
     * @param str
     * @param fromIndexInclude
     * @param toIndexExclude
     * @return
     */
    public static String sub(CharSequence str, int fromIndexInclude, int toIndexExclude) {
        if (isEmpty(str)) {
            return str(str);
        }
        int len = str.length();

        if (fromIndexInclude < 0) {
            fromIndexInclude = len + fromIndexInclude;
            if (fromIndexInclude < 0) {
                fromIndexInclude = 0;
            }
        } else if (fromIndexInclude > len) {
            fromIndexInclude = len;
        }

        if (toIndexExclude < 0) {
            toIndexExclude = len + toIndexExclude;
            if (toIndexExclude < 0) {
                toIndexExclude = len;
            }
        } else if (toIndexExclude > len) {
            toIndexExclude = len;
        }

        if (toIndexExclude < fromIndexInclude) {
            int tmp = fromIndexInclude;
            fromIndexInclude = toIndexExclude;
            toIndexExclude = tmp;
        }

        if (fromIndexInclude == toIndexExclude) {
            return EMPTY;
        }

        return str.toString().substring(fromIndexInclude, toIndexExclude);
    }
}
